<!--
  -    Copyright (c) 2018-2025, bfcloud All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: bfcloud
  -->
<template>
  <div class="execution">
    <basic-container>
      <div>
        <el-button type="primary" plain @click="openBtn">新增</el-button>
        <el-button type="primary" plain @click="getList">刷新</el-button>
      </div>
      <el-table
        ref="table"
        style="width: 100%; margin-top: 20px"
        :data="tableData"
        border
      >
        <el-table-column
          type="index"
          width="80"
          header-align="center"
          align="center"
          label="序号"
        ></el-table-column>
        <el-table-column
          prop="faqiName"
          header-align="center"
          align="center"
          min-width="200"
          show-overflow-tooltip
          label="发起名称"
        >
        </el-table-column>
        <el-table-column
          prop="rwYxqq"
          header-align="center"
          align="center"
          min-width="160"
          show-overflow-tooltip
          label="任务开始时间"
        >
        </el-table-column>
        <el-table-column
          prop="rwYxqz"
          header-align="center"
          align="center"
          min-width="160"
          show-overflow-tooltip
          label="任务结束时间"
        >
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          min-width="100"
          label="启动"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              v-if="scope.row.state != '2'"
              active-color="#999999"
              inactive-color="#17499a"
              active-value="1"
              inactive-value="0"
              @change="stateChange(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          min-width="100"
          label="结束"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              v-if="scope.row.state != '1'"
              active-color="green"
              inactive-color="red"
              active-value="0"
              inactive-value="2"
              @change="stateChange(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="stateExplain"
          header-align="center"
          align="center"
          min-width="100"
          label="任务状态"
        >
        </el-table-column>
        <el-table-column
          header-align="center"
          fixed="right"
          label="操作"
          min-width="120"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleRoll(scope.row)"
              v-if="scope.row.state == '0'"
            >滚动下达</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="changeObj(scope.row)"
              v-if="scope.row.state === '1'"
              >编辑</el-button
            >
            <el-button
              @click="rowDel(scope.row)"
              type="text"
              size="small"
              v-if="scope.row.state === '1'"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        title="任务管理"
        :close-on-click-modal="false"
        :visible.sync="zdrwvisible"
        width="30%"
        top="8vh"
      >
        <el-row>
          <el-form
            :model="dataForm"
            ref="dataForm"
            :rules="dataRule"
            label-width="130px"
            class="demo-ruleForm"
          >
            <div>
              <el-row>
                <el-col :span="24">
                    <el-form-item label="发起名称" prop="faqiName">
                      <el-input
                        v-model="dataForm.faqiName"
                        placeholder="发起名称"
                      ></el-input>
                    </el-form-item>
                    <el-form-item label="开始日期" prop="rwYxqq">
                        <el-date-picker
                          v-model="dataForm.rwYxqq"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="开始日期"
                          style="width: 100%"
                        ></el-date-picker>

                    </el-form-item>
                    <el-form-item label="结束日期" prop="rwYxqz">
                       <el-date-picker
                          v-model="dataForm.rwYxqz"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="结束日期"
                          style="width: 100%"
                        ></el-date-picker>
                    </el-form-item>
                  </el-col>
              </el-row>
            </div>
          </el-form>
        </el-row>

        <span slot="footer" class="dialog-footer">
          <el-button type="primary" plain @click="saveFormData('dataForm')"
            >保存</el-button
          >
          <el-button type="primary" plain @click="zdrwvisible = false"
            >关闭</el-button
          >
        </span>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import {
  fetchList,
  addObj,
  putObj,
  putObjstateChange,
  delObj,
  renwugundong
} from "@/api/hetongrenwu/zhongdianrenwu/zhongdianrwcj/index";
export default {
  name: "renwuqidong",
  data() {
    var checkDate = (rule, value, callback) => {
      if (value) {
        if (
          new Date(value.replace(/-/g, "/")) >
          new Date(this.dataForm.rwYxqz.replace(/-/g, "/"))
        ) {
          callback("开始日期不能大于结束日期!");
        } else {
          callback();
        }
      } else {
        callback("请选择开始日期");
      }
    };
    var checkDateKai = (rule, value, callback) => {
      if (value) {
        if (
          new Date(value.replace(/-/g, "/")) <
          new Date(this.dataForm.rwYxqq.replace(/-/g, "/"))
        ) {
          callback("结束日期不能小于开始日期!");
        } else {
          callback();
        }
      } else {
        callback("请选择结束日期");
      }
    };
    return {
      dataRule: {
        rwYxqz: [{required: true, validator: checkDateKai, trigger: "change" }],
        rwYxqq:[{required: true, validator:checkDate, trigger: "blur"}],
        faqiName: [
          { required: true, message: "发起名称不能为空", trigger: "blur" },
        ],
      },
      titles: ["未使用", "已使用"],
      zdrwvisible: false,
      dialogVisible: false,
      templaterAllList: [],
      templaterUseList: [],
      searchForm: {},
      dataForm: {
        faqiName: "",
        rwYxqq: "",
        rwYxqz: "",
      },
      tableData: [],
      orgCode: "",
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
      },
      tableLoading: false,
    };
  },
  created() {
    this.getList(this.page);
  },
  methods: {
    openBtn(){
      this.zdrwvisible = true;
      this.dataForm = {};
    },
    getList(page, params) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize,
          },
          params,
          this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    rowDel(row) {
      this.$confirm("是否确认删除名称为【" + row.faqiName + "】的任务吗？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(row.id);
        })
        .then((data) => {
          this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
    //滚动下达
    handleRoll(row){
      var _this=this;
      this.$confirm("是否确认对名称为【" + row.faqiName + "】的任务进行滚动下达？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          _this.openLoading();
          const params={
            rwId:row.id
          }
          return renwugundong(params);
        })
        .then((data) => {
          console.log(data)
          if(data.data.code==0){
            this.$message.closeAll();
            this.$message.success("滚动下达成功");
            this.getList(this.page);
          }else{
            this.$message.warning(data.data.msg);
          }
          this.$loading().close();
        }).catch((err)=>{
          console.log(err)
        this.$loading().close();
      });
    },
    changeObj(row) {
      this.dataForm.id = row.id;
      this.dataForm.faqiName = row.faqiName;
      this.dataForm.rwYxqq = row.rwYxqq;
      this.dataForm.rwYxqz = row.rwYxqz;
      this.zdrwvisible = true;
    },
    stateChange: function (row) {
      var params = {
        id: row.id,
        state: row.state,
      };
      putObjstateChange(params)
        .then((data) => {
          if (data.data.data === "-1") {
            this.$message.warning(data.data.msg);
          } else {
            this.$message.success("操作成功");
          }
          this.getList(this.page);
          done();
        })
        .catch(() => {
          // loading();
        });
    },
    saveFormData(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addObj(this.dataForm).then((data) => {
            if (data.data.data === "-1") {
              this.$message.warning(data.data.msg);
            } else {
              this.zdrwvisible = false;
              this.$message.success("操作成功");
              this.getList(this.page);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    refreshChange() {
      this.getList(this.page);
    },
    //提交loading
    openLoading(loadText) {
      loadText=loadText?loadText:'加载';
      const loading = this.$loading({
        lock: true,
        text: "正在"+loadText+"，请稍等...",
        spinner: "el-icon-loading",
        background: "rgba(255, 255, 255, 0.7)",
      });
    },
  },
};
</script>
